package com.example.security;

import com.example.entity.SysUser;
import com.example.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现用户登录查库处理
 */

@Service                            //把Service对象存入spring容器中
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUser sysUser = sysUserService.getByUsername(username);       //通过username获取此username的对象

        if(sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码不正确");
        }

        //返回一个自定义user对象，对象的属性由sysUser赋值而来
        return new AccountUser(sysUser.getId(),sysUser.getUsername(),sysUser.getPassword(),
                getUserAuthority(sysUser.getId()));
    }

    /**
     * 通过userId获取用户权限信息(角色，菜单权限)
     * @param userId
     * @return
     */
    public List<GrantedAuthority> getUserAuthority(Long userId) {

        String authority = sysUserService.getUserAuthorityInfo(userId);

        return AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
    }

}
